
#define _AA_TABLE_C
#include "aa_table.h"

#include <string.h>
#include <math.h>
/**\ingroup AATable*/

const struct aa_entry aa_table[AA_COUNT] = {
   /*            1      2      3      4      5      6      7      8      9      0      1      2      3      4      5      6      7 */
  {"ALA", 'A',{/*0,*/ /*1,*/   1,     0,   /*0,*/ /*1,*/ /*0,*/   0,   /*1,*/   0,     0,     0,     1,     0,     0,     1,     0}},
  {"ARG", 'R',{/*0,*/ /*1,*/   0,     0,   /*1,*/ /*0,*/ /*1,*/   0,   /*0,*/   1,     1,     0,     0,     1,     1,     0,     1}},

  {"ASN", 'N',{/*0,*/ /*1,*/   0,     0,   /*0,*/ /*0,*/ /*0,*/   0,   /*0,*/   0,     1,     0,     1,     1,     0,     0,     1}},
  {"ASP", 'D',{/*1,*/ /*1,*/   0,     0,   /*0,*/ /*0,*/ /*1,*/   0,   /*0,*/   0,     1,     1,     0,     1,     0,     0,     1}},

  {"CYS", 'C',{/*0,*/ /*1,*/   0,     0,   /*0,*/ /*1,*/ /*0,*/   0,   /*0,*/   0,     0,     0,     1,     1,     0,     0,     0}},
  {"GLU", 'E',{/*1,*/ /*1,*/   0,     0,   /*0,*/ /*0,*/ /*1,*/   0,   /*0,*/   1,     0,     1,     0,     1,     0,     0,     1}},

   /*            1      2      3      4      5      6      7      8      9      0      1      2      3      4      5      6      7 */


  {"GLN", 'Q',{/*0,*/ /*1,*/   0,     0,   /*0,*/ /*0,*/ /*0,*/   0,   /*0,*/   1,     0,     0,     1,     1,     0,     0,     1}},
  {"GLY", 'G',{/*0,*/ /*1,*/   1,     0,   /*0,*/ /*0,*/ /*0,*/   0,   /*1,*/   0,     0,     0,     1,     0,     0,     1,     1}},

  {"HIS", 'H',{/*0,*/ /*0,*/   0,     1,   /*1,*/ /*0,*/ /*1,*/   1,   /*0,*/   1,     0,     0,     0,     1,     1,     0,     1}},
  {"ILE", 'I',{/*0,*/ /*1,*/   1,     0,   /*0,*/ /*1,*/ /*0,*/   0,   /*1,*/   1,     0,     0,     1,     0,     0,     0,     0}},

  {"LEU", 'L',{/*0,*/ /*1,*/   1,     0,   /*0,*/ /*1,*/ /*0,*/   0,   /*1,*/   1,     0,     0,     1,     0,     0,     0,     0}},
  {"LYS", 'K',{/*0,*/ /*1,*/   0,     0,   /*1,*/ /*0,*/ /*1,*/   0,   /*0,*/   1,     0,     0,     0,     1,     1,     0,     1}},

   /*            1      2      3      4      5      6      7      8      9      0      1      2      3      4      5      6      7 */

  {"MET", 'M',{/*0,*/ /*1,*/   0,     0,   /*0,*/ /*1,*/ /*0,*/   0,   /*1,*/   1,     0,     0,     1,     0,     0,     0,     0}},
  {"PHE", 'F',{/*0,*/ /*0,*/   0,     1,   /*0,*/ /*1,*/ /*0,*/   1,   /*1,*/   1,     0,     0,     1,     0,     0,     0,     0}},

  {"PRO", 'P',{/*0,*/ /*0,*/   0,     0,   /*0,*/ /*0,*/ /*0,*/   1,   /*1,*/   0,     1,     0,     1,     0,     0,     0,     1}},
  {"SER", 'S',{/*0,*/ /*1,*/   0,     0,   /*0,*/ /*0,*/ /*0,*/   0,   /*0,*/   0,     0,     0,     1,     1,     0,     1,     1}},

  {"THR", 'T',{/*0,*/ /*1,*/   0,     0,   /*0,*/ /*0,*/ /*0,*/   0,   /*0,*/   0,     1,     0,     1,     1,     0,     0,     1}},
  {"TRP", 'W',{/*0,*/ /*0,*/   0,     1,   /*0,*/ /*1,*/ /*0,*/   1,   /*1,*/   1,     0,     0,     1,     0,     0,     0,     0}},

   /*            1      2      3      4      5      6      7      8      9      0      1      2      3      4      5      6      7 */

  {"TYR", 'Y',{/*0,*/ /*0,*/   0,     1,   /*0,*/ /*0,*/ /*0,*/   1,   /*1,*/   1,     0,     0,     1,     0,     0,     0,     1}},
  {"VAL", 'V',{/*0,*/ /*1,*/   1,     0,   /*0,*/ /*1,*/ /*0,*/   0,   /*1,*/   0,     1,     0,     1,     0,     0,     0,     0}}
};

/*

http://www.inrp.fr/Acces/biotic/rastop/help/aatable.htm

Here's a text version of the original table from the web:

Residues:  ala  arg  asn  asp  cys  glu  gln  gly  his  ile  leu  lys  met  phe  pro  ser  thr  trp  tyr  val
  Atom Set  A R N D C E Q G H I L K M F P S T W Y V
1 acidic          *   *                            
2 acyclic   * * * * * * * *   * * * *     * *     *
3 aliphatic *             *   * *                 *
4 aromatic                  *         *       * *  
  Atom Set  A R N D C E Q G H I L K M F P S T W Y V
5 basic       *             *     *                 
6 buried    *       *         * *   * *       *   *
7 charged     *   *   *     *     *                
8 cyclic                    *         * *     * *  
  Atom Set  A R N D C E Q G H I L K M F P S T W Y V
9 hydrophob *             *   * *   * * *     * * *
0 large       *       * *   * * * * * *       * *  
1 medium        * * *                   *   *     *
2 negative        *   *                            
  Atom Set  A R N D C E Q G H I L K M F P S T W Y V
3 neutral   *   *   *   * * * * *   * * * * * * * *  <-H Can't be positive and neutral and charged.  Set neutral to false.
4 polar       * * * * * *   *     *       * *      
5 positive    *             *     *                
6 small     *             *               *        
  Atom Set  A R N D C E Q G H I L K M F P S T W Y V
7 surface     * * *   * * * *     *     * * *   *  

*/


char aa_get_short_abbrev(const char long_abbrev[3]) {
  int i;
  for(i = 0; i < AA_COUNT; i++) {
    if(!strncmp(aa_table[i].long_abbrev, long_abbrev, 3)) {
      return aa_table[i].short_abbrev;
    }
  }
  return '\0';
}

int aa_get_table_offset(const char short_abbrev) {
  int i;
  for(i =0 ; i< AA_COUNT; i++) {
    if(short_abbrev == aa_table[i].short_abbrev) {
      return i;
    }
  }
  return -1;
}

/**\ingroup AATable*/
#define NULL_S "\0\0\0\0"
/**\ingroup AATable*/
const struct aa_active_site_def aa_active_sites[AA_COUNT]= {

  {"ALA", 'A', NULL_S, NULL_S},
  {"ARG", 'R', " NH1", " NH2"},

  {"ASN", 'N', " OD1", NULL_S},
  {"ASP", 'D', " OD1", " OD2"},

  {"CYS", 'C', " SG ", NULL_S},
  {"GLU", 'E', " OE1", " OE2"},


  {"GLN", 'Q', " OE1", NULL_S},
  {"GLY", 'G', NULL_S, NULL_S},

  {"HIS", 'H', " NE2", NULL_S},
  {"ILE", 'I', NULL_S, NULL_S},

  {"LEU", 'L', NULL_S, NULL_S},
  {"LYS", 'K', " NZ ", NULL_S},


  {"MET", 'M', NULL_S, NULL_S},
  {"PHE", 'F', " CD1", " CE2"},

  {"PRO", 'P', NULL_S, NULL_S},
  {"SER", 'S', " OG ", NULL_S},

  {"THR", 'T', " OG1", NULL_S},
  {"TRP", 'W', " CD2", " CE2"},

  {"TYR", 'Y', " OH ", NULL_S},
  {"VAL", 'V', NULL_S, NULL_S},
};

#undef NULL_S

// Polar - Non-polar (hydrophobic)
/**\ingroup AATable*/
#define po_hy  2.0  
/**\ingroup AATable*/
#define hy_po  2.0  
// Positive - Negative 
/**\ingroup AATable*/
#define pl_mi  2.0 
/**\ingroup AATable*/
#define mi_pl  2.0 
// Any other differences
/**\ingroup AATable*/
#define po_pl 1.0
/**\ingroup AATable*/
#define po_mi 1.0
// ar is aromatic.
/**\ingroup AATable*/
#define po_ar 1.0   

/**\ingroup AATable*/
#define hy_pl 1.0
/**\ingroup AATable*/
#define hy_mi 1.0
/**\ingroup AATable*/
#define hy_ar 1.0   

/**\ingroup AATable*/
#define mi_po 1.0
/**\ingroup AATable*/
#define mi_hy 1.0
/**\ingroup AATable*/
#define mi_ar 1.0

/**\ingroup AATable*/
#define pl_po 1.0
/**\ingroup AATable*/
#define pl_hy 1.0
/**\ingroup AATable*/
#define pl_ar 1.0

/**\ingroup AATable*/
#define ar_po 1.0
/**\ingroup AATable*/
#define ar_hy 1.0
/**\ingroup AATable*/
#define ar_mi 1.0
/**\ingroup AATable*/
#define ar_pl 1.0

/**\ingroup AATable*/
#define ar_ar 0.0
/**\ingroup AATable*/
#define po_po 0.0
/**\ingroup AATable*/
#define hy_hy 0.0
/**\ingroup AATable*/
#define pl_pl 0.0
/**\ingroup AATable*/
#define mi_mi 0.0


/**\ingroup AATable*/
const double aa_type_distances [AA_COUNT][AA_COUNT]= { 

      // ALA    ARG    ASN    ASP    CYS    GLU    GLN    GLY    HIS    ILE    LEU    LYS    MET    PHE    PRO    SER    THR    TRP    TYR    VAL
      
//ALA 
  {      hy_hy, hy_pl, hy_po, hy_mi, hy_po, hy_mi, hy_po, hy_hy, hy_pl, hy_hy, hy_hy, hy_pl, hy_hy, hy_ar, hy_hy, hy_po, hy_po, hy_ar, hy_po, hy_hy },
     
//ARG
  {      pl_hy, pl_pl, pl_po, pl_mi, pl_po, pl_mi, pl_po, pl_hy, pl_pl, pl_hy, pl_hy, pl_pl, pl_hy, pl_ar, pl_hy, pl_po, pl_po, pl_ar, pl_po, pl_hy },

//ASN
  {      po_hy, po_pl, po_po, po_mi, po_po, po_mi, po_po, po_hy, po_pl, po_hy, po_hy, po_pl, po_hy, po_ar, po_hy, po_po, po_po, po_ar, po_po, po_hy },

//ASP  
  {      mi_hy, mi_pl, mi_po, mi_mi, mi_po, mi_mi, mi_po, mi_hy, mi_pl, mi_hy, mi_hy, mi_pl, mi_hy, mi_ar, mi_hy, mi_po, mi_po, mi_ar, mi_po, mi_hy },

//CYS  
  {      po_hy, po_pl, po_po, po_mi, po_po, po_mi, po_po, po_hy, po_pl, po_hy, po_hy, po_pl, po_hy, po_ar, po_hy, po_po, po_po, po_ar, po_po, po_hy },

//GLU 
  {      mi_hy, mi_pl, mi_po, mi_mi, mi_po, mi_mi, mi_po, mi_hy, mi_pl, mi_hy, mi_hy, mi_pl, mi_hy, mi_ar, mi_hy, mi_po, mi_po, mi_ar, mi_po, mi_hy },

//GLN 
  {      po_hy, po_pl, po_po, po_mi, po_po, po_mi, po_po, po_hy, po_pl, po_hy, po_hy, po_pl, po_hy, po_ar, po_hy, po_po, po_po, po_ar, po_po, po_hy },

//GLY 
  {      hy_hy, hy_pl, hy_po, hy_mi, hy_po, hy_mi, hy_po, hy_hy, hy_pl, hy_hy, hy_hy, hy_pl, hy_hy, hy_ar, hy_hy, hy_po, hy_po, hy_ar, hy_po, hy_hy },

//HIS 
  {      pl_hy, pl_pl, pl_po, pl_mi, pl_po, pl_mi, pl_po, pl_hy, pl_pl, pl_hy, pl_hy, pl_pl, pl_hy, pl_ar, pl_hy, pl_po, pl_po, pl_ar, pl_po, pl_hy },

//ILE 
  {      hy_hy, hy_pl, hy_po, hy_mi, hy_po, hy_mi, hy_po, hy_hy, hy_pl, hy_hy, hy_hy, hy_pl, hy_hy, hy_ar, hy_hy, hy_po, hy_po, hy_ar, hy_po, hy_hy },

//LEU 
  {      hy_hy, hy_pl, hy_po, hy_mi, hy_po, hy_mi, hy_po, hy_hy, hy_pl, hy_hy, hy_hy, hy_pl, hy_hy, hy_ar, hy_hy, hy_po, hy_po, hy_ar, hy_po, hy_hy },

//LYS 
  {      pl_hy, pl_pl, pl_po, pl_mi, pl_po, pl_mi, pl_po, pl_hy, pl_pl, pl_hy, pl_hy, pl_pl, pl_hy, pl_ar, pl_hy, pl_po, pl_po, pl_ar, pl_po, pl_hy },

//MET 
  {      hy_hy, hy_pl, hy_po, hy_mi, hy_po, hy_mi, hy_po, hy_hy, hy_pl, hy_hy, hy_hy, hy_pl, hy_hy, hy_ar, hy_hy, hy_po, hy_po, hy_ar, hy_po, hy_hy },

//PHE 
  {      ar_hy, ar_pl, ar_po, ar_mi, ar_po, ar_mi, ar_po, ar_hy, ar_pl, ar_hy, ar_hy, ar_pl, ar_hy, ar_ar, ar_hy, ar_po, ar_po, ar_ar, ar_po, ar_hy },

//PRO 
  {      hy_hy, hy_pl, hy_po, hy_mi, hy_po, hy_mi, hy_po, hy_hy, hy_pl, hy_hy, hy_hy, hy_pl, hy_hy, hy_ar, hy_hy, hy_po, hy_po, hy_ar, hy_po, hy_hy },

//SER 
  {      po_hy, po_pl, po_po, po_mi, po_po, po_mi, po_po, po_hy, po_pl, po_hy, po_hy, po_pl, po_hy, po_ar, po_hy, po_po, po_po, po_ar, po_po, po_hy },

//THR 
  {      po_hy, po_pl, po_po, po_mi, po_po, po_mi, po_po, po_hy, po_pl, po_hy, po_hy, po_pl, po_hy, po_ar, po_hy, po_po, po_po, po_ar, po_po, po_hy },

//TRP 
  {      ar_hy, ar_pl, ar_po, ar_mi, ar_po, ar_mi, ar_po, ar_hy, ar_pl, ar_hy, ar_hy, ar_pl, ar_hy, ar_ar, ar_hy, ar_po, ar_po, ar_ar, ar_po, ar_hy },

//TYR 
  {      po_hy, po_pl, po_po, po_mi, po_po, po_mi, po_po, po_hy, po_pl, po_hy, po_hy, po_pl, po_hy, po_ar, po_hy, po_po, po_po, po_ar, po_po, po_hy },

//VAL
  {      hy_hy, hy_pl, hy_po, hy_mi, hy_po, hy_mi, hy_po, hy_hy, hy_pl, hy_hy, hy_hy, hy_pl, hy_hy, hy_ar, hy_hy, hy_po, hy_po, hy_ar, hy_po, hy_hy }

};

/**\ingroup AATable*/
const double aa_hydrophobicity[AA_COUNT] = {

  0.31, //  {"ALA", 'A', NULL_S, NULL_S}, 
 -1.01, //  {"ARG", 'R', " NH1", " NH2"},

 -0.60, //  {"ASN", 'N', " OD1", NULL_S},
 -0.77, //  {"ASP", 'D', " OD1", " OD2"},
  
  1.54, //  {"CYS", 'C', " SG ", NULL_S},
 -0.64, //  {"GLU", 'E', " OE1", " OE2"},


 -0.22, //  {"GLN", 'Q', " OE1", NULL_S},
  0.00, //  {"GLY", 'G', NULL_S, NULL_S},

  0.13, //  {"HIS", 'H', " NE2", NULL_S},
  1.80, //  {"ILE", 'I', NULL_S, NULL_S},

  1.70, //  {"LEU", 'L', NULL_S, NULL_S},
 -0.99, //  {"LYS", 'K', " NZ ", NULL_S},


  1.23, //  {"MET", 'M', NULL_S, NULL_S},
  1.79, //  {"PHE", 'F', " CD1", " CE2"},

  0.72, //  {"PRO", 'P', NULL_S, NULL_S},
 -0.04, //  {"SER", 'S', " OG ", NULL_S},

  0.26, //  {"THR", 'T', " OG1", NULL_S},
  2.25, //  {"TRP", 'W', " CD2", " CE2"},

  0.96, //  {"TYR", 'Y', " OH ", NULL_S},
  1.22  //  {"VAL", 'V', NULL_S, NULL_S},
};

double aa_type_distance(char t1, char t2) {
  
  int i = aa_get_table_offset(t1);
  int j = aa_get_table_offset(t2);

  if(i == -1 || j == -1) {
    return -1;
  }

  return aa_type_distances[i][j];
}

double aa_type_hydrophobicity(char t) {
  return aa_hydrophobicity[aa_get_table_offset(t)];
}


int blosum62[24][24] = {                     
                        
  /*      A   R   N   D   C   E   Q   G   H   I   L   K   M   F   P   S   T   W   Y   V   B   Z   X   * */
  {/*A*/  4, -1, -2, -2,  0, -1, -1,  0, -2, -1, -1, -1, -1, -2, -1,  1,  0, -3, -2,  0, -2, -1,  0, -4},
  {/*R*/ -1,  5,  0, -2, -3,  0,  1, -2,  0, -3, -2,  2, -1, -3, -2, -1, -1, -3, -2, -3, -1,  0, -1, -4},
  {/*N*/ -2,  0,  6,  1, -3,  0,  0,  0,  1, -3, -3,  0, -2, -3, -2,  1,  0, -4, -2, -3,  3,  0, -1, -4},
  {/*D*/ -2, -2,  1,  6, -3,  2,  0, -1, -1, -3, -4, -1, -3, -3, -1,  0, -1, -4, -3, -3,  4,  1, -1, -4},
  {/*C*/  0, -3, -3, -3,  9, -4, -3, -3, -3, -1, -1, -3, -1, -2, -3, -1, -1, -2, -2, -1, -3, -3, -2, -4},
  {/*E*/ -1,  0,  0,  2, -4,  5,  2, -2,  0, -3, -3,  1, -2, -3, -1,  0, -1, -3, -2, -2,  1,  4, -1, -4},
  {/*Q*/ -1,  1,  0,  0, -3,  2,  5, -2,  0, -3, -2,  1,  0, -3, -1,  0, -1, -2, -1, -2,  0,  3, -1, -4},
  {/*G*/  0, -2,  0, -1, -3, -2, -2,  6, -2, -4, -4, -2, -3, -3, -2,  0, -2, -2, -3, -3, -1, -2, -1, -4},
  {/*H*/ -2,  0,  1, -1, -3,  0,  0, -2,  8, -3, -3, -1, -2, -1, -2, -1, -2, -2,  2, -3,  0,  0, -1, -4},
  {/*I*/ -1, -3, -3, -3, -1, -3, -3, -4, -3,  4,  2, -3,  1,  0, -3, -2, -1, -3, -1,  3, -3, -3, -1, -4},
  {/*L*/ -1, -2, -3, -4, -1, -3, -2, -4, -3,  2,  4, -2,  2,  0, -3, -2, -1, -2, -1,  1, -4, -3, -1, -4},
  {/*K*/ -1,  2,  0, -1, -3,  1,  1, -2, -1, -3, -2,  5, -1, -3, -1,  0, -1, -3, -2, -2,  0,  1, -1, -4},
  {/*M*/ -1, -1, -2, -3, -1, -2,  0, -3, -2,  1,  2, -1,  5,  0, -2, -1, -1, -1, -1,  1, -3, -1, -1, -4},
  {/*F*/ -2, -3, -3, -3, -2, -3, -3, -3, -1,  0,  0, -3,  0,  6, -4, -2, -2,  1,  3, -1, -3, -3, -1, -4},
  {/*P*/ -1, -2, -2, -1, -3, -1, -1, -2, -2, -3, -3, -1, -2, -4,  7, -1, -1, -4, -3, -2, -2, -1, -2, -4},
  {/*S*/  1, -1,  1,  0, -1,  0,  0,  0, -1, -2, -2,  0, -1, -2, -1,  4,  1, -3, -2, -2,  0,  0,  0, -4},
  {/*T*/  0, -1,  0, -1, -1, -1, -1, -2, -2, -1, -1, -1, -1, -2, -1,  1,  5, -2, -2,  0, -1, -1,  0, -4},
  {/*W*/ -3, -3, -4, -4, -2, -3, -2, -2, -2, -3, -2, -3, -1,  1, -4, -3, -2, 11,  2, -3, -4, -3, -2, -4},
  {/*Y*/ -2, -2, -2, -3, -2, -2, -1, -3,  2, -1, -1, -2, -1,  3, -3, -2, -2,  2,  7, -1, -3, -2, -1, -4},
  {/*V*/  0, -3, -3, -3, -1, -2, -2, -3, -3,  3,  1, -2,  1, -1, -2, -2,  0, -3, -1,  4, -3, -2, -1, -4},
  {/*B*/ -2, -1,  3,  4, -3,  1,  0, -1,  0, -3, -4,  0, -3, -3, -2,  0, -1, -4, -3, -3,  4,  1, -1, -4},
  {/*Z*/ -1,  0,  0,  1, -3,  4,  3, -2,  0, -3, -3,  1, -1, -3, -1,  0, -1, -3, -2, -2,  1,  4, -1, -4},
  {/*X*/  0, -1, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2,  0,  0, -2, -1, -1, -1, -1, -1, -4},
  {/***/ -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,  1}
};                        
                        


